﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using Newtonsoft.Json;

namespace DAL
{
    public  class DAL_CMS_Category
    {
        static KQCMS db = new KQCMS();
        /// <summary>
        /// 查询所有栏目
        /// </summary>
        /// <returns></returns>
        public static List<CMS_Category> seleAll_CMS_Category()
        {
            return db.CMS_Category.ToList();
        }
        //
        /// <summary>
        /// 新增栏目
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static int Insert(CMS_Category c)
        {
            using (var db = new KQCMS())
            {
                var stu = new CMS_Category() { ctitle=c.ctitle, cname=c.cname, nav=c.nav, navorder=c.navorder, search=c.search };
                db.CMS_Category.Add(stu);
                return db.SaveChanges();
            }

        }
        //
        /// <summary>
        /// 修改栏目
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static int update_CMS_Category(int id,string ctitle,string cname,bool nav,int navorder,bool search)
        {
            using (var db=new KQCMS()) {
                var i=db.CMS_Category.Where(x=>x.cid==id).FirstOrDefault();
                i.cid=id;
                i.ctitle=ctitle;
                i.cname=cname;
                i.nav=nav;
                i.navorder=navorder;
                i.search=search;
                return db.SaveChanges();
            }
           
        }
        //
        /// <summary>
        /// 单值查询
        /// </summary>
        /// <returns></returns>
        public static List<CMS_Category> Selectid(int id)
        {
            using (var db = new KQCMS())
            {
                var a = db.CMS_Category.Where(x => x.cid == id).ToList();
                return a;
            }

        }
        //
        /// <summary>
        /// 删除栏目
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static int Datete(int id)
        {
            using (var db = new KQCMS())
            {
                var stu = new CMS_Category() { cid = id };
                db.CMS_Category.Attach(stu);
                db.CMS_Category.Remove(stu);
                return db.SaveChanges();
            }

        }
        //
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetPageCMS_Articles(int pageindex, int pagesize, string cid)
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Category.AsQueryable();
                //所有数据
                //list = list.Where(x => x.state == 1 || x.state == 2);
                if (!string.IsNullOrEmpty(cid))
                {
                    int id = Convert.ToInt32(cid);
                    list = list.Where(x => x.cid == id);
                }
                //某一页数据
                var data = list.OrderBy(x => x.cid)
               .Skip((pageindex - 1) * pagesize)//排除前面多少行
               .Take(pagesize)//只取前面多行
               .ToList();             //得到数据的条数

                //总行数
                int rowCount = list.ToList().Count();
                //总页数
                var pageSum = 0;
                //通过计算，得到分页应该需要分几页，其中不满一页的数据按一页计算
                if (rowCount % pagesize != 0)
                {
                    pageSum = rowCount / pagesize + 1;
                }
                else
                {
                    pageSum = rowCount / pagesize;
                }

                //转成Json格式    总页数：9页                  当前页码                                           当前页的数据              
                var strResult = "{\"pageSum\":" + pageSum + ",\"CurrentPage\":" + pageindex + ",\"list\":" + JsonConvert.SerializeObject(data) + "}";
                return strResult;
            }
        }
    }
}